//
//  WYApplyMemberController.m
//  CirclePush
//
//  Created by admin on 2018/4/16.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYApplyMemberController.h"
#import "IDAuthViewController.h"
@interface WYApplyMemberController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *topHeight;
@property (strong, nonatomic) IBOutlet UIView *mainView;
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;

@property (strong, nonatomic) IBOutlet UITextField *userName;
@property (strong, nonatomic) IBOutlet UITextField *userMobile;
@property (strong, nonatomic) IBOutlet UITextField *bankTextfield;

@property (strong, nonatomic) IBOutlet UIImageView *cardPositive;
@property (nonatomic,strong)NSString *cardPositiveString;
@property (strong, nonatomic) IBOutlet UIImageView *cardBack;
@property (nonatomic,strong)NSString *cardBackString;
@property (strong, nonatomic) IBOutlet UIImageView *bankPositive;
@property (nonatomic,strong)NSString *bankPositiveString;
@property (strong, nonatomic) IBOutlet UIImageView *bankBack;
@property (nonatomic,strong)NSString *bankBackString;
@property (nonatomic,strong)NSString *type;

@end

@implementation WYApplyMemberController

- (void)viewDidLoad {
    [super viewDidLoad];
    if (kDevice_Is_iPhoneX) {
        self.topHeight.constant = 98;
    }else{
        self.topHeight.constant = 74;
    }
    [self setupUI];
    [self addAction];
}
-(void)setupUI
{
    [self initTopBarWithTitle:@"申请推客"];
    [self addLeftButton:[UIImage imageNamed:@"return"] width:0 height:0];
    self.view.backgroundColor = [UIColor hexStringToColor:@"#f4f4f4" alpha:1.0];
    self.mainView.width = kScreenWidth;
    self.mainView.height = kScreenHeight;
    
    [self.scrollView addSubview:self.mainView];
    [self.scrollView setContentSize:self.mainView.size];
}
-(void)addAction
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickImageViewAction:)];
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickImageViewAction:)];
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickImageViewAction:)];
    UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickImageViewAction:)];
    self.cardPositive.userInteractionEnabled  = YES;
    [self.cardPositive addGestureRecognizer:tap];
    self.cardBack.userInteractionEnabled  = YES;
    [self.cardBack addGestureRecognizer:tap1];
    self.bankPositive.userInteractionEnabled  = YES;
    [self.bankPositive addGestureRecognizer:tap2];
    self.bankBack.userInteractionEnabled  = YES;
    [self.bankBack addGestureRecognizer:tap3];
}

-(void)clickImageViewAction:(UITapGestureRecognizer *)ges
{
    self.type = [NSString stringWithFormat:@"%ld",ges.view.tag];
    NSLog(@"%@",self.type);
    [self openImagePickerController];
    
}


-(void)openImagePickerController
{
    UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init];
//    imagePicker.editing = YES;
    imagePicker.delegate = self;
    imagePicker.allowsEditing = YES;
    //选择相机时，设置UIImagePickerController对象相关属性
   
//    imagePicker.modalPresentationStyle = UIModalPresentationFullScreen;
//    imagePicker.mediaTypes = @[(NSString *)kUTTypeImage];
//    imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
    //跳转到UIImagePickerController控制器弹出相机
   
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:(UIAlertControllerStyleActionSheet)];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
         imagePicker.sourceType =  UIImagePickerControllerSourceTypeCamera;
         [self presentViewController:imagePicker animated:YES completion:nil];
    }];
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        imagePicker.sourceType =  UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:imagePicker animated:YES completion:nil];
    }];
    [alertController addAction:action1];
    [alertController addAction:action2];
    [alertController addAction:action3];
    [self presentViewController:alertController animated:YES completion:nil];
    
    
  
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    UIImage *image=[info objectForKey:UIImagePickerControllerEditedImage];
    //    self.saveimageView.image = image1;
  //  self.photoImage.image = image1;
    //选取完图片之后关闭视图
    [self upLoadImage:image];
    [self showLoading:@"上传中..."];
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

-(void)upLoadImage:(UIImage *)image
{
    switch (self.type.intValue) {
        case 1:
            self.cardPositive.image = image;
            break;
        case 2:
            self.cardBack.image = image;
            break;
        case 3:
            self.bankPositive.image = image;
            break;
        case 4:
            self.bankBack.image = image;
            break;
        default:
            break;
    }
    WEAKSELF
  //  NSData *data =UIImagePNGRepresentation(image);
    NSData *data = UIImageJPEGRepresentation(image, 0.3);
    [WYHttpServer upLoadImage:data SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
//     NSString *str =   [[NSString alloc]initWithData:response encoding:(NSUTF8StringEncoding)];
//        NSLog(@"%@",str);
        [self hideLoading];
        if (success) {
            switch (self.type.intValue) {
                case 1:
                    weakSelf.cardPositiveString = response[@"msg"];
                    break;
                case 2:
                    weakSelf.cardBackString = response[@"msg"];
                    break;
                    
                case 3:
                    weakSelf.bankPositiveString = response[@"msg"];
                    break;
                    
                case 4:
                    weakSelf.bankBackString = response[@"msg"];
                    break;
                default:
                    break;
            }
            
            
        }else{
            
            [weakSelf toast:errorMessage];
            
        }
       
        
        
    }];
    
    
    
}







-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)sureAction:(id)sender {
    
    if (!self.userName.text.length) {
        [self toast:@"请输入姓名"];return;
    }
    if (!self.userMobile.text.length) {
        [self toast:@"请输入手机号"];return;
    }
    if (!self.bankTextfield.text.length) {
        [self toast:@"请输入银行卡号"];return;
    }
    if (!self.cardPositiveString.length) {
        [self toast:@"没有身份证正面"];return;
    }
    if (!self.cardBackString.length) {
        [self toast:@"没有身份证反面"];return;
    }
    if (!self.bankPositiveString.length) {
        [self toast:@"没有银行卡正面"];return;
    }
    if (!self.bankBackString.length) {
        [self toast:@"没有银行卡反面"];return;
    }
    WEAKSELF
    [WYHttpServer AddTwitterWithName:self.userName.text WithTelephone:self.userMobile.text WithBankCard:self.bankTextfield.text WithCardFront:self.cardPositiveString WithCartVerso:self.cardBackString WithBankRight:self.bankBackString WithBankback:self.bankPositiveString SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        
        if (success) {
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }else{
            [weakSelf toast:errorMessage];
        }
        
        
        
        
    }];
    
    
    
    
    
}



@end
